!! Copyright (C) 2009,2010,2011,2012  Marco Restelli
!!
!! This file is part of:
!!   LDGH -- Local Hybridizable Discontinuous Galerkin toolkit
!!
!! LDGH is free software: you can redistribute it and/or modify it
!! under the terms of the GNU General Public License as published by
!! the Free Software Foundation, either version 3 of the License, or
!! (at your option) any later version.
!!
!! LDGH is distributed in the hope that it will be useful, but WITHOUT
!! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
!! or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
!! License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with LDGH. If not, see <http://www.gnu.org/licenses/>.
!!
!! author: Marco Restelli                   <marco.restelli@gmail.com>


module mod_octave_io_sparse
!General comments: octave io for sparse matrices. Overloads the
!mod_octave_io subroutines for the types of mod_sparse.
!-----------------------------------------------------------------------

!-----------------------------------------------------------------------

 use mod_messages, only: &
   mod_messages_initialized, &
   error,   &
   warning, &
   info

 use mod_kinds, only: &
   mod_kinds_initialized, &
   wp

 use mod_octave_io, only: &
   mod_octave_io_initialized, &
   write_octave, &
   real_format

 use mod_sparse, only: &
   mod_sparse_initialized, &
   t_col, t_tri, &
   tri2col

!-----------------------------------------------------------------------
 
 implicit none

!-----------------------------------------------------------------------

! Module interface

 public :: &
   mod_octave_io_sparse_constructor, &
   mod_octave_io_sparse_destructor,  &
   mod_octave_io_sparse_initialized, &
   write_octave

 private

!-----------------------------------------------------------------------

! Module types and parameters

! Module variables

 ! public members
 logical, protected ::               &
   mod_octave_io_sparse_initialized = .false.

 character(len=*), parameter :: &
   this_mod_name = 'mod_octave_io_sparse'

 interface write_octave
   module procedure write_col
 end interface

!-----------------------------------------------------------------------

contains

!-----------------------------------------------------------------------

 subroutine mod_octave_io_sparse_constructor()
  character(len=*), parameter :: &
    this_sub_name = 'constructor'

   !Consistency checks ---------------------------
   if( (mod_messages_initialized.eqv..false.) .or. &
       (mod_kinds_initialized.eqv..false.)    .or. &
       (mod_octave_io_initialized.eqv..false.).or. &
       (mod_sparse_initialized.eqv..false.) ) then
     call error(this_sub_name,this_mod_name, &
                'Not all the required modules are initialized.')
   endif
   if(mod_octave_io_sparse_initialized.eqv..true.) then
     call warning(this_sub_name,this_mod_name, &
                  'Module is already initialized.')
   endif
   !----------------------------------------------

   mod_octave_io_sparse_initialized = .true.
 end subroutine mod_octave_io_sparse_constructor

!-----------------------------------------------------------------------
 
 subroutine mod_octave_io_sparse_destructor()
  character(len=*), parameter :: &
    this_sub_name = 'destructor'
   
   !Consistency checks ---------------------------
   if(mod_octave_io_sparse_initialized.eqv..false.) then
     call error(this_sub_name,this_mod_name, &
                'This module is not initialized.')
   endif
   !----------------------------------------------

   mod_octave_io_sparse_initialized = .false.
 end subroutine mod_octave_io_sparse_destructor

!-----------------------------------------------------------------------
 
 subroutine write_col(m,var_name,fu)
  integer, intent(in) :: fu
  type(t_col), intent(in) :: m
  character(len=*), intent(in) :: var_name
 
  integer :: j,p
  character(len=8+len(real_format)) :: out_format
  character(len=*), parameter :: &
    this_sub_name = 'write_col'

   write(fu,'(a,a)')    '# name: ',var_name
   write(fu,'(a)')      '# type: sparse matrix'
   write(fu,'(a,i15)')  '# nnz: '    ,m%nz
   write(fu,'(a,i7)')   '# rows: '   ,m%n
   write(fu,'(a,i7)')   '# columns: ',m%m

   write(out_format,'(a,a,a)') '(i7,i7,', real_format, ')'
   coldo: do j=1,m%m ! column loop
     rowdo: do p=m%ap(j)+1,m%ap(j+1)
       write(fu,out_format) m%ai(p)+1 , j , m%ax(p)
     enddo rowdo
   enddo coldo
 
 end subroutine write_col
 
!-----------------------------------------------------------------------
 
 subroutine write_tri(t,var_name,fu)
  integer, intent(in) :: fu
  type(t_tri), intent(in) :: t
  character(len=*), intent(in) :: var_name
 
  character(len=*), parameter :: &
    this_sub_name = 'write_tri'

   call write_octave(tri2col(t),var_name,fu)
 
 end subroutine write_tri
 
!-----------------------------------------------------------------------

end module mod_octave_io_sparse

